

import 'package:flutter/material.dart';
import 'custom_extended_image_provider.dart';
import 'package:extended_image/extended_image.dart';

class CustomExtendedImage extends StatelessWidget {
  const CustomExtendedImage.network({required this.url,
    Key? key,
    this.semanticLabel,
    this.excludeFromSemantics = false,
    this.width,
    this.height,
    this.color,
    this.colorBlendMode,
    this.fit,
    this.alignment = Alignment.center,
    this.repeat = ImageRepeat.noRepeat,
    this.centerSlice,
    this.matchTextDirection = false,
    this.gaplessPlayback = false,
    this.filterQuality = FilterQuality.low,
    this.loadStateChanged,
    this.shape,
    this.border,
    this.borderRadius,
    this.clipBehavior = Clip.antiAlias,
    this.enableLoadState = true,
    this.beforePaintImage,
    this.afterPaintImage,
    this.mode = ExtendedImageMode.none,
    this.enableMemoryCache = true,
    this.clearMemoryCacheIfFailed = true,
    this.onDoubleTap,
    this.initGestureConfigHandler,
    this.enableSlideOutPage = false,
    this.constraints,
    this.cancelToken,
    this.retries = 0,
    this.timeLimit,
    this.headers,
    this.cache = true,
    this.scale = 1.0,
    this.timeRetry = const Duration(milliseconds: 100),
    this.extendedImageEditorKey,
    this.initEditorConfigHandler,
    this.heroBuilderForSlidingPage,
    this.clearMemoryCacheWhenDispose = false,
    this.handleLoadingProgress = false,
    this.extendedImageGestureKey,
    this.cacheWidth,
    this.cacheHeight,
    this.isAntiAlias = false,
    this.cacheKey,
    this.printError = true,
    this.compressionRatio,
    this.maxBytes,
    this.cacheRawData = false,
    this.imageCacheName,
    this.cacheMaxAge
  });
  
  final String url;
  
  final Key? extendedImageGestureKey;

  final bool handleLoadingProgress;
  
  final bool clearMemoryCacheWhenDispose;
  
  final HeroBuilderForSlidingPage? heroBuilderForSlidingPage;
  
  final InitEditorConfigHandler? initEditorConfigHandler;
  
  final Key? extendedImageEditorKey;
  
  final bool enableSlideOutPage;
  
  final InitGestureConfigHandler? initGestureConfigHandler;
  
  final DoubleTap? onDoubleTap;
  
  final bool enableMemoryCache;
  final bool clearMemoryCacheIfFailed;
  
  final ExtendedImageMode mode;
  
  final BeforePaintImage? beforePaintImage;

  final AfterPaintImage? afterPaintImage;

  final bool enableLoadState;
  final Clip clipBehavior;
  final BoxShape? shape;
  final BoxBorder? border;
  
  final BorderRadius? borderRadius;
  
  final LoadStateChanged? loadStateChanged;
  
  final double? width;
  final double? height;
  
  final Color? color;
  final FilterQuality filterQuality;
  
  final BlendMode? colorBlendMode;
  final BoxFit? fit;
  
  final Alignment alignment;
  final ImageRepeat repeat;
  final Rect? centerSlice;
  final bool matchTextDirection;
  final bool gaplessPlayback;
  final String? semanticLabel;
  final bool excludeFromSemantics;
  final bool isAntiAlias;

  final BoxConstraints? constraints;
  final CancellationToken? cancelToken;
  final int retries;
  final Duration? timeLimit;
  final Map<String, String>? headers;
  final bool cache;
  final double scale;
  final Duration timeRetry;
  final int? cacheWidth;
  final int? cacheHeight;
  final String? cacheKey;
  final bool printError;
  final double? compressionRatio;
  final int? maxBytes;
  final bool cacheRawData;
  final String? imageCacheName;
  final Duration? cacheMaxAge;

  @override
  Widget build(BuildContext context){
    
    return ExtendedImage(
        semanticLabel: semanticLabel,
        excludeFromSemantics: excludeFromSemantics,
        width: width,
        height: height,
        color: color,
        colorBlendMode: colorBlendMode,
        fit: fit,
        alignment: alignment,
        repeat: repeat,
        centerSlice: centerSlice,
        matchTextDirection: matchTextDirection,
        gaplessPlayback: gaplessPlayback,
        filterQuality: filterQuality,
        border: border,
        shape: shape,
        borderRadius: borderRadius,
        clipBehavior: clipBehavior,
        enableLoadState: enableLoadState,
        beforePaintImage: beforePaintImage,
        afterPaintImage: afterPaintImage,
        mode: mode,
        loadStateChanged: loadStateChanged ?? (state) {
          if (state.extendedImageLoadState != LoadState.completed) {
            return Container(
                width: width,
                height: height,
                color: Colors.black12
            );
          }
          return null;
        },
        enableMemoryCache: enableMemoryCache,
        clearMemoryCacheIfFailed: clearMemoryCacheIfFailed,
        onDoubleTap: onDoubleTap,
        initGestureConfigHandler: initGestureConfigHandler,
        enableSlideOutPage: enableSlideOutPage,
        extendedImageEditorKey: extendedImageEditorKey,
        initEditorConfigHandler: initEditorConfigHandler,
        heroBuilderForSlidingPage: heroBuilderForSlidingPage,
        clearMemoryCacheWhenDispose: clearMemoryCacheWhenDispose,
        extendedImageGestureKey: extendedImageGestureKey,
        isAntiAlias: isAntiAlias,
        handleLoadingProgress: handleLoadingProgress,
      image: ExtendedResizeImage.resizeIfNeeded(
        provider: CustomExtendedNetworkImageProvider(
          url,
          scale: scale,
          headers: headers,
          orgUrl: url,
          cache: cache,
          cancelToken: cancelToken,
          retries: retries,
          timeRetry: timeRetry,
          timeLimit: timeLimit,
          cacheKey: cacheKey,
          printError: printError,
          cacheRawData: cacheRawData,
          imageCacheName: imageCacheName,
          cacheMaxAge: cacheMaxAge,
        ),
        compressionRatio: compressionRatio,
        maxBytes: maxBytes,
        cacheWidth: cacheWidth,
        cacheHeight: cacheHeight,
        cacheRawData: cacheRawData,
        imageCacheName: imageCacheName,
      )
    );
  }
}